import {
  Component,
  OnChanges,
  OnInit,
  SimpleChanges,
  Input
} from "@angular/core";
import { HttpServcie } from "../../../service/http.service";
import { Router } from "@angular/router";
import { NzNotificationService } from "ng-zorro-antd";
import { Observable } from "rxjs/Observable";

@Component({
  selector: "superviser-ele",
  templateUrl: "superviser-ele.html",
  styleUrls: ["superviser-ele.scss"]
})
export class SuperviserEle implements OnInit, OnChanges {
  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService
  ) {}
  @Input() partyId: string = "";

  option1 = {};
  option2 = {};
  option4 = {};
  option5 = {};
  option6 = {};
  option7 = {};
  maintNum: any;
  usedNum: any;
  maintCzryTotal: any;
  usedCzryTotal: any;
  elevatorTypeData: any = [];
  numlist = [];
  elevatorTotal: any;

  ngOnInit() {}

  ngOnChanges(changes: SimpleChanges) {
    this.countLifeOlderYoung(); //十年以上一下电梯
    this.countEleTypeNum(); //各电梯类型 电梯数量
    this.countEleNum(); //电梯数量
  }

  countEleNum() {
    let param = {
      id: this.partyId
    };
    this.httpServcie.countEleNum(param).subscribe(
      data => {
        if (data.status === 200) {
          this.numlist = data.data;
          let nameList = [];
          let memberList = [];
          this.numlist.forEach(element => {
            if (element.id == this.partyId) {
              this.elevatorTotal = element.total_count;
            } else {
              let total_count = element.total_count;
              memberList.push({
                name: element.party_name,
                value: total_count
              });
              nameList.push(element.party_name);
            }
          });

          this.option4 = {
            title: {
              text: "辖区电梯分布图",
              subtext: "",
              x: "center"
            },
            tooltip: {
              trigger: "item",
              formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
              orient: "vertical",
              left: "left",
              data: nameList
            },
            series: [
              {
                name: "台",
                type: "pie",
                radius: "30%",
                center: ["45%", "50%"],
                data: memberList,
                itemStyle: {
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: "rgba(0, 0, 0, 0.5)"
                  }
                }
              }
            ]
          };
        } else if (data.status === 401) {
          this.router.navigateByUrl("/login");
          return;
        } else {
          // 异常
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        console.log(JSON.stringify(error));
      }
    );
  }

  countLifeOlderYoung() {
    let param = {
      id: this.partyId
    };
    Observable.forkJoin(
      this.httpServcie.countLifeOlder(param),
      this.httpServcie.countLifeYounger(param)
    ).subscribe(data => {
      let elevatorYearData = [];
      if (data[0].status == 200) {
        elevatorYearData.push({
          value: data[0].data[0].total_count,
          name: "十年以上"
        });
      } else {
        elevatorYearData.push({
          value: 0,
          name: "十年以上"
        });
      }
      if (data[1].status == 200) {
        elevatorYearData.push({
          value: data[1].data[0].total_count,
          name: "十年以下"
        });
      } else {
        elevatorYearData.push({
          value: 0,
          name: "十年以下"
        });
      }

      this.option2 = {
        title: {
          text: "电梯年龄",
          subtext: "",
          x: "center"
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
          orient: "vertical",
          left: "left",
          data: ["十年以上", "十年以下"]
        },
        series: [
          {
            name: "电梯",
            type: "pie",
            radius: "30%",
            center: ["45%", "50%"],
            data: elevatorYearData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      };
    });
  }

  countEleTypeNum() {
    let param = {
      id: this.partyId
    };
    this.httpServcie.countEleTypeNum(param).subscribe(data => {
      let elevatorTypeList = data.data;
      if (elevatorTypeList) {
        elevatorTypeList.forEach(elevatorType => {
          if (
            elevatorType.id == this.partyId &&
            elevatorType.elevator_type_id == 1
          ) {
            this.elevatorTypeData.push({
              value: elevatorType.total_count,
              name: "载客电梯"
            });
          }
          if (
            elevatorType.id == this.partyId &&
            elevatorType.elevator_type_id == 5
          ) {
            this.elevatorTypeData.push({
              value: elevatorType.total_count,
              name: "载货电梯"
            });
          }
          if (
            elevatorType.id == this.partyId &&
            elevatorType.elevator_type_id == 2
          ) {
            this.elevatorTypeData.push({
              value: elevatorType.total_count,
              name: "液压驱动电梯"
            });
          }
          if (
            elevatorType.id == this.partyId &&
            elevatorType.elevator_type_id == 3
          ) {
            this.elevatorTypeData.push({
              value: elevatorType.total_count,
              name: "杂物电梯"
            });
          }
          if (
            elevatorType.id == this.partyId &&
            elevatorType.elevator_type_id == 4
          ) {
            this.elevatorTypeData.push({
              value: elevatorType.total_count,
              name: "自动扶梯"
            });
          }
          if (
            elevatorType.id == this.partyId &&
            elevatorType.elevator_type_id == 6
          ) {
            this.elevatorTypeData.push({
              value: elevatorType.total_count,
              name: "自动人行道"
            });
          }
        });
      }

      this.option1 = {
        title: {
          text: "电梯类型",
          subtext: "",
          x: "center"
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
          orient: "vertical",
          left: "left",
          data: [
            "载客电梯",
            "载货电梯",
            "自动扶梯",
            "自动人行道",
            "液压驱动电梯",
            "杂物电梯"
          ]
        },
        series: [
          {
            name: "电梯",
            type: "pie",
            radius: "30%",
            center: ["45%", "50%"],
            data: this.elevatorTypeData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      };
    });
  }
}
